﻿#include "freindwidget.h"
#include "ui_freindwidget.h"
#include "chatsocket.h"
#include <QIcon>
#include <QButtonGroup>
#include "friendrequestlist.h"

FreindWidget::FreindWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FreindWidget)
{
    ui->setupUi(this);
    QIcon icon = QIcon("res/Icon/joker2.png");
    ui->tool_Icon->setIcon(icon);
    ui->listWidget->setSpacing(20);
    ui->listWidget->setWrapping(true);
    setShadow();
    ui->comboBox_sex->addItems(QStringList()<<"不限"<<"男"<<"女");
    ui->comboBox_age->addItems(QStringList()<<"不限"<<"0-18岁"<<"19-36岁"<<"37-60岁"<<"61岁及以上");
    ui->comboBox_age->setCurrentIndex(-1);
    ui->comboBox_sex->setCurrentIndex(-1);
    ui->comboBox_age->installEventFilter(this);
    ui->comboBox_sex->installEventFilter(this);
    connect(ChatSocket::getInstance(),&ChatSocket::transSearchResult,this,&FreindWidget::showSearchResult);
    QButtonGroup* g1 = new QButtonGroup(this);
    g1->addButton(ui->radioButton,0);
    g1->addButton(ui->radioButton_2,1);
    ui->radioButton->setChecked(true);
    ui->btn_RList->setIcon(QIcon("res/Icon/oldf.png"));
}

FreindWidget::~FreindWidget()
{
    for (auto obj : m_listItemVec) {
        if(obj != nullptr)
            delete obj;
    }
    delete ui;
}

void FreindWidget::showSearchResult(Json::Value &root)
{
    int resultType = root["ResultType"].asInt();//0表示用户信息
    QString id = ChatSocket::getInstance()->getUserID();
    if(0 == resultType)
    {
        for(int i = 0; i < root["info"].size(); ++i)
        {
            UserInfo userinfo;
            userinfo.id = root["info"][i]["ID"].asCString();
//            if(userinfo.id == id)continue;
            userinfo.nickname = root["info"][i]["NickName"].asCString();
            userinfo.sex = root["info"][i]["Sex"].asCString();
            userinfo.age = root["info"][i]["Age"].asCString();
            userinfo.headurl = root["info"][i]["HeadUrl"].asCString();
            userinfo.persig = root["info"][i]["PerSig"].asCString();
            userinfo.praise = root["info"][i]["Praise"].asCString();
            userinfo.birthday = root["info"][i]["BirthDay"].asCString();
            userinfo.star = root["info"][i]["Star"].asCString();
            userinfo.address = root["info"][i]["Address"].asCString();
            userinfo.phone = root["info"][i]["Phone"].asCString();
            userinfo.rubric = root["info"][i]["Rubric"].asCString();
            userinfo.zodiac = root["info"][i]["Zodiac"].asCString();
            m_map[userinfo.id] = userinfo;
        }
        QString path = QCoreApplication::applicationDirPath();
        for (auto obj : m_map)
        {
            MyListWidgetItem* mwi = new MyListWidgetItem(this,0);
            QString style = QString("min-width:  80px;max-width:  80px;min-height: 80px;max-height: 80px;border-radius: 40px;border-width: 1 1 1 1;border-image: url(%1) 0 0 0 0 stretch strectch;").arg(obj.second.headurl);
            mwi->setStyle(style,obj.second);
            if(obj.second.id == id)
                mwi->setBtnText("查看资料");
            if(!MainWidget::getInstance()->queryIsFriend(obj.second.id))
                mwi->setBtnText("发消息");
            QListWidgetItem * item = new QListWidgetItem(ui->listWidget);
            item->setSizeHint(QSize(300,110));
            ui->listWidget->addItem(item);
            ui->listWidget->setItemWidget(item,mwi);
            m_listItemVec.append(mwi);
        }
    }
    else
    {
        for(int i = 0; i < root["info"].size(); ++i)
        {
            GroupInfo ginfo;
            ginfo.id = root["info"][i]["GID"].asCString();
            ginfo.perNums = root["info"][i]["GPerNum"].asCString();
            ginfo.headurl = root["info"][i]["GHead"].asCString();
            ginfo.nickname = root["info"][i]["GName"].asCString();
            m_map2[ginfo.id] = ginfo;
        }
        QString path = QCoreApplication::applicationDirPath();
        for (auto obj : m_map2)
        {
            MyListWidgetItem* mwi = new MyListWidgetItem(this,1);
            QString style = QString("min-width:  80px;max-width:  80px;min-height: 80px;max-height: 80px;border-radius: 40px;border-width: 1 1 1 1;border-image: url(%1) 0 0 0 0 stretch strectch;").arg(obj.second.headurl);
            mwi->setStyle(style,obj.second);
            QListWidgetItem * item = new QListWidgetItem(ui->listWidget);
            item->setSizeHint(QSize(300,110));
            ui->listWidget->addItem(item);
            ui->listWidget->setItemWidget(item,mwi);
            m_listItemVec.append(mwi);
        }
    }
}

bool FreindWidget::eventFilter(QObject *obj, QEvent *event)
{
    if(obj == ui->comboBox_age && event->type() == QEvent::MouseMove)
        return true;
    if(obj == ui->comboBox_sex && event->type() == QEvent::MouseMove)
        return true;
    return QWidget::eventFilter(obj,event);
}

void FreindWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::LeftButton)
    {
        isPressedWidget = true; // 当前鼠标按下的是QWidget而非界面上布局的其它控件
        last = event->globalPos();
    }
}

void FreindWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (isPressedWidget)
    {
        int dx = event->globalX() - last.x();
        int dy = event->globalY() - last.y();
        last = event->globalPos();
        move(x()+dx, y()+dy);
    }
}

void FreindWidget::mouseReleaseEvent(QMouseEvent *event)
{
    int dx = event->globalX() - last.x();
    int dy = event->globalY() - last.y();
    move(x()+dx, y()+dy);
    isPressedWidget = false; // 鼠标松开时，置为false
}

void FreindWidget::setShadow()
{
    //设置无边框
    setWindowFlags(Qt::FramelessWindowHint);
    //设置窗体透明
    setAttribute(Qt::WA_TranslucentBackground, true);
    QGraphicsDropShadowEffect *shadow=new QGraphicsDropShadowEffect();
    shadow->setBlurRadius(15);//设置圆角半径
    shadow->setColor(QColor(0,0,0));//设置边框颜色
    shadow->setOffset(0,0);
    ui->widget->setStyleSheet("background-color:white");
    ui->widget->setGraphicsEffect(shadow);
}

void FreindWidget::setIcon(int type)
{
    QIcon icon;
    if(type == 0)
    {
        icon.addFile("res/Icon/oldf.png");
    }
    else
    {
        icon.addFile("res/Icon/newf.png");
    }
    ui->btn_RList->setIcon(icon);
}

void FreindWidget::on_btn_close_clicked()
{
    close();
}

void FreindWidget::on_btn_min_clicked()
{
    showMinimized();
}

void FreindWidget::on_comboBox_sex_currentIndexChanged(const QString &arg1)
{
    ui->lineEdit_sex->setText(arg1);
}

void FreindWidget::on_comboBox_age_currentIndexChanged(const QString &arg1)
{
    ui->lineEdit_age->setText(arg1);
}

void FreindWidget::on_btn_find_clicked()//查找好友
{
//    int count = ui->listWidget->count();
//    for(int i = 0; i < count; i++)
//    {
//        auto obj = ui->listWidget->takeItem(i);
//        delete obj;
//    }
    ui->listWidget->clear();
    m_map.clear();
    QString key("");
    int ageIndex = -1, sexIndex = -1;
    if(ui->lineEdit->text().size())
    {
        key = ui->lineEdit->text();
    }
    Json::Value root;
    root["TYPE"] = SEARCH_USER;
    root["key"] = key.toStdString();
    ageIndex = ui->comboBox_age->currentIndex();
    root["ageLimit"] = ageIndex;
    sexIndex = ui->comboBox_sex->currentIndex();
    root["sexLimit"] = sexIndex;
    if(ui->radioButton->isChecked())
    {
        root["FindType"] = 0;
    }
    else if(ui->radioButton_2->isChecked())
    {
        root["FindType"] = 1;
    }
    ChatSocket::getInstance()->sendChatData(root);
}

void FreindWidget::on_btn_RList_clicked()
{
    FriendRequestList::getInstance()->show();
}
